package com.example.bootcurriculumproject.testController;

import com.example.bootcurriculumproject.service.ShipService;
import com.example.bootcurriculumproject.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;

@Transactional
@SpringBootTest
@Slf4j
@AutoConfigureMockMvc
public class testShipController {
    @MockBean
    private ShipService shipService;
    @MockBean
    private RedisUtil redisUtil;
    @Autowired
    private MockMvc mockMvc;
    private static final String URL = "/ship";
    @Test
    public void testAddShip() throws Exception {
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        //控制类测试
        Mockito.when(shipService.save(any())).thenReturn(true);
        String body = "{\"addressnum\": 1111111,\"uid\": \"bbbbb\",\"consignee\": \"aadddcc\",\"region\": \"gfbfbnreb\",\"postalcode\": 23355,\"address\": \"bbccdd\",\"phone\": 88899222}";
        //  String exceptReturn = "{\"id\":10,\"question\":\"ssss的问题1\",\"answer\":\"回答问题\"}";
        String result = this.mockMvc.perform(MockMvcRequestBuilders.post(URL+"/addShip")
                        .accept(MediaType.APPLICATION_JSON)
                        .content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                //.andExpect(MockMvcResultMatchers.status().isCreated())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
    @Test
    public void testDeleteShip() throws Exception {
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.doNothing().when(shipService).deleteShip(any());
        String body = "{\"addressnum\": 1111111,\"uid\": \"bbbbb\",\"consignee\": \"aadddcc\",\"region\": \"gfbfbnreb\",\"postalcode\": 23355,\"address\": \"bbccdd\",\"phone\": 88899222}";
        //  String exceptReturn = "{\"id\":10,\"question\":\"ssss的问题1\",\"answer\":\"回答问题\"}";
        String result = this.mockMvc.perform(MockMvcRequestBuilders.delete(URL+"/deleteShip/1111")
                        .accept(MediaType.APPLICATION_JSON)
                        //.content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                //.andExpect(MockMvcResultMatchers.status().isCreated())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
    @Test
    public void testUpdateUser() throws Exception {
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.doNothing().when(shipService).updateShip(any());
        String body = "{\"addressnum\": 1111111,\"uid\": \"bbbbb\",\"consignee\": \"aadddcc\",\"region\": \"gfbfbnreb\",\"postalcode\": 23355,\"address\": \"bbccdd\",\"phone\": 88899222}";
        //  String exceptReturn = "{\"id\":10,\"question\":\"ssss的问题1\",\"answer\":\"回答问题\"}";
        String result = this.mockMvc.perform(MockMvcRequestBuilders.put(URL+"/updateShip")
                        .accept(MediaType.APPLICATION_JSON)
                        .content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                //.andExpect(MockMvcResultMatchers.status().isCreated())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
}
